package com.xy.annotaion.handler.kotlin.graph

import org.jfree.chart.ChartFactory
import org.jfree.chart.ChartPanel
import org.jfree.chart.JFreeChart
import org.jfree.chart.axis.NumberAxis
import org.jfree.chart.block.BlockBorder
import org.jfree.chart.plot.CategoryPlot
import org.jfree.chart.renderer.category.BarRenderer
import org.jfree.chart.title.TextTitle
import org.jfree.chart.ui.ApplicationFrame
import org.jfree.chart.ui.UIUtils
import org.jfree.data.category.CategoryDataset
import org.jfree.data.category.DefaultCategoryDataset
import java.awt.Color
import java.awt.Dimension


/**
 * A simple demonstration application showing how to create a bar chart.
 */
class BarChartDemo1(title: String?) : ApplicationFrame(title) {
    companion object {
        private const val serialVersionUID = 1L

        /**
         * Returns a sample dataset.
         *
         * @return The dataset.
         */
        private fun createDataset(): CategoryDataset {
            val dataset = DefaultCategoryDataset()
            dataset.addValue(7445.0, "JFreeSVG", "Warm-up")
            dataset.addValue(24448.0, "Batik", "Warm-up")
            dataset.addValue(4297.0, "JFreeSVG", "Test")
            dataset.addValue(21022.0, "Batik", "Test")
            return dataset
        }

        /**
         * Creates a sample chart.
         *
         * @param dataset  the dataset.
         *
         * @return The chart.
         */
        private fun createChart(dataset: CategoryDataset): JFreeChart {
            val chart = ChartFactory.createBarChart(
                "Performance: JFreeSVG vs Batik", null /* x-axis label*/,
                "Milliseconds" /* y-axis label */, dataset
            )
            chart.addSubtitle(
                TextTitle(
                    "Time to generate 1000 charts in SVG "
                            + "format (lower bars = better performance)"
                )
            )
            chart.backgroundPaint = Color.WHITE
            val plot = chart.plot as CategoryPlot

            // ******************************************************************
            //  More than 150 demo applications are included with the JFreeChart
            //  Developer Guide...for more information, see:
            //
            //  >   http://www.object-refinery.com/jfreechart/guide.html
            //
            // ******************************************************************
            val rangeAxis: NumberAxis = plot.rangeAxis as NumberAxis
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits())
            val renderer = plot.renderer as BarRenderer
            renderer.isDrawBarOutline = false
            chart.legend.frame = BlockBorder.NONE
            return chart
        }

    }

    /**
     * Creates a new demo instance.
     *
     * @param title  the frame title.
     */
    init {
        val dataset = createDataset()
        val chart = createChart(dataset)
        val chartPanel = ChartPanel(chart, false)
        chartPanel.fillZoomRectangle = true
        chartPanel.isMouseWheelEnabled = true
        chartPanel.preferredSize = Dimension(500, 270)
        contentPane = chartPanel
    }
}

fun main() {
    val demo = BarChartDemo1("JFreeChart: BarChartDemo1.java")
    demo.pack()
    UIUtils.centerFrameOnScreen(demo)
    demo.isVisible = true
}